package cn.com.zcode.core.util;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class StringUtil {

	public static StringBuffer append(StringBuffer sb, String str, String separator) {
		if (sb == null) {
			sb = new StringBuffer();
		} else {
			sb.append(separator);
		}
		sb.append(str);
		return sb;
	}

	public static String getMd5Value(String str) {
		try {
			MessageDigest messageDigest = MessageDigest.getInstance("MD5");
			messageDigest.update(str.getBytes());
			byte[] byteArray = messageDigest.digest();
			StringBuffer md5StrBuff = new StringBuffer();

			for (int i = 0; i < byteArray.length; i++) {
				if (Integer.toHexString(0xFF & byteArray[i]).length() == 1)
					md5StrBuff.append("0").append(Integer.toHexString(0xFF & byteArray[i]));
				else
					md5StrBuff.append(Integer.toHexString(0xFF & byteArray[i]));
			}
			return md5StrBuff.toString().toUpperCase();
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		}
		return null;
	}

	public static String trim(String str) {
		return (str != null) ? str.trim() : str;
	}

	public static String formatString(Object string, int length) {
		return String.format("%-" + length + "s", string);
	}

	public static boolean isEmpty( String string) {
		return string == null||string.isEmpty();
	}
	 
}
